package com.example.ch02_student;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import com.example.ch02_student.adminsDo.AdminsDo;
import com.example.ch02_student.entity.Admins;
import com.example.ch02_student.utils.RoleInfo;
import com.example.ch02_student.utils.UrlBase;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;

import java.io.IOException;
import java.security.PrivateKey;
import java.util.Date;

import lombok.SneakyThrows;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class AdminsMain extends AppCompatActivity {
    EditText no,name,address1,tel1,age1,email,yz;//验证码
    private RadioButton sex1,sex2;

    private static String ano,aname,sex,address,tel;
    private static int age;
    OkHttpClient okHttpClient;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_admins_main);
        init();
        okHttpClient=new OkHttpClient();
        //初始化主页面传来的数据
        Bundle bundle = getIntent().getExtras();
        if (bundle!=null){
         ano = bundle.getString("ano");
         aname=bundle.getString("aname");
         sex=bundle.getString("sex");
         age=bundle.getInt("age");
         address=bundle.getString("address");
         tel=bundle.getString("tel");
        }
        //修改身份
        RoleInfo.INT_ROLE=1;
        //赋值
        no.setText(ano);
        name.setText(aname);
        if ("男".equals(sex)){
            sex1.setChecked(true);
        }else {
            sex2.setChecked(true);
        }
        //"男".equals(sex)?sex1.setChecked(true):sex2.setChecked(true);

        age1.setText(age+"");
        address1.setText(address);
        tel1.setText(tel);
    }

    //请求邮箱接口
    public void goEmail(View view){
        String s = email.getText().toString();
        System.err.println(s);
        Request build = new Request.Builder().url(UrlBase.URL_BASE+"/users/send?email="+s).build();
        Call call = okHttpClient.newCall(build);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {

            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                    if (response.isSuccessful()){
                        System.err.println("success");
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(AdminsMain.this,"验证发送成功",Toast.LENGTH_SHORT).show();
                            }
                        });
                    }else {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(AdminsMain.this,"error",Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
            }
        });
    }

    //修改信息
    public void goUpdate(View view){
        //验证码

        String s = yz.getText().toString();
        FormBody formBody = new FormBody.Builder().add("no", ano).add("s", s).build();
        //post请求
        Admins admins = new Admins(ano, name.getText().toString(), sex1.getText().toString(), Integer.parseInt(age1.getText().toString()), address1.getText().toString(), tel1.getText().toString(), null);
        String s1 = new Gson().toJson(admins);

        RequestBody requestBody = RequestBody.create(s1, MediaType.parse("application/json"));
        Request build = new Request.Builder().url(UrlBase.URL_BASE+"/users/updateUser?no="+ano+"&s="+s).post(requestBody).build();
        Call call = okHttpClient.newCall(build);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {

            }
            @SneakyThrows
            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                String string = response.body().string();
                JSONObject jsonObject = new JSONObject(string);
                if (jsonObject!=null&&jsonObject.getInt("code")==1000){
                    System.err.println("success");
                    aname=name.getText().toString();
                    //设置性别
                    if (sex1.isChecked()){
                        sex=sex1.getText().toString();
                    }else {
                        sex=sex2.getText().toString();
                    }
                    age=Integer.parseInt(age1.getText().toString());
                    address=address1.getText().toString();
                    tel= tel1.getText().toString();

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            //改变静态变量
                            fuzhi();
                            Toast.makeText(AdminsMain.this,"修改成功",Toast.LENGTH_SHORT).show();
                        }
                    });
                }else {
                    Looper.prepare();
                    Toast.makeText(AdminsMain.this,"验证码错误",Toast.LENGTH_SHORT).show();
                    Looper.loop();
                }

            }
        });
    }
    //跳转操作页面选项
    public void admindo(View view){
        Intent intent = new Intent(AdminsMain.this, AdminsDo.class);
        startActivity(intent);
    }

    //初始化控件
    public void init(){
        no=findViewById(R.id.no);
        name=findViewById(R.id.name);
        sex1=findViewById(R.id.sex1);
        sex2=findViewById(R.id.sex2);
        address1=findViewById(R.id.address);
        tel1=findViewById(R.id.tel);
        age1=findViewById(R.id.age);
        email=findViewById(R.id.email);
        yz=findViewById(R.id.yz);//验证码


    }

    //渲染数据
    public void fuzhi(){
        no.setText(ano);
        name.setText(aname);
        if ("男".equals(sex)){
            sex1.setChecked(true);
        }else {
            sex2.setChecked(true);
        }
        age1.setText(age+"");
        address1.setText(address);
        tel1.setText(tel);
    }
}
